package com.xmjs.baselibrary.view.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;

import com.xmjs.baselibrary.R;

/**
 * 部分文本添加下划线，并可响应点击
 */
public class AutoLinkStyleTextView extends androidx.appcompat.widget.AppCompatTextView {

    private static int linkColor = Color.parseColor("#333333");
    private String defaultTextValue = null;
    private boolean hasUnderLine = true;

    private ClickCallBack mClickCallBack;

    public AutoLinkStyleTextView(Context context) {
        this(context, null);
    }

    public AutoLinkStyleTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoLinkStyleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AutoLinkStyleTextView, defStyleAttr, 0);
        defaultTextValue = typedArray.getString(R.styleable.AutoLinkStyleTextView_link_text);
        linkColor = typedArray.getColor(R.styleable.AutoLinkStyleTextView_link_color, linkColor);
        hasUnderLine = typedArray.getBoolean(R.styleable.AutoLinkStyleTextView_has_under_line, hasUnderLine);
        addStyle();
        typedArray.recycle();
    }

    /**
     * 部分文字链接的通过xml设置静态渲染
     */
    private void addStyle() {
        if (!TextUtils.isEmpty(defaultTextValue) && defaultTextValue.contains("。")) {
            String[] values = defaultTextValue.split("。");
            SpannableString spannableString = new SpannableString(getText().toString().trim());
            for (int i = 0; i < values.length; i++) {
                final int position = i;
                spannableString.setSpan(new ClickableSpan() {
                                            @Override
                                            public void onClick(View widget) {
                                                if (mClickCallBack != null) mClickCallBack.onClick(position);
                                            }

                                            @Override
                                            public void updateDrawState(TextPaint ds) {
                                                super.updateDrawState(ds);
                                                ds.setColor(linkColor);
                                                ds.setUnderlineText(hasUnderLine);
                                            }
                                        }, getText().toString().trim().indexOf(values[i]),
                        getText().toString().trim().indexOf(values[i]) + values[i].length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            setText(spannableString);
            setMovementMethod(LinkMovementMethod.getInstance());
        }
    }

    public void setOnClickCallBack(ClickCallBack clickCallBack) {
        this.mClickCallBack = clickCallBack;
    }

    public interface ClickCallBack {
        void onClick(int position);
    }
}
